import { uploadAssetApi, createNewAsset } from "../api/uploadAsset";
import {
  getDefaultImageFormatStyle
} from "@/logics/shortcutkey";
// 完成保存网址操作, 这个封面图是可视区域的截图
export const saveUrlLB = async requestData => {
  console.log("开始保存网址", requestData);
  let formatStyle = await getDefaultImageFormatStyle()
  const screenshotUrl = await chrome.tabs.captureVisibleTab(null, {format: formatStyle});
  let params = {
    srcUrl: requestData.href,
    base: screenshotUrl
  };
  console.log("params", params);
  const { code, data } = await uploadAssetApi(params);
  console.log("上传base64", code, data);
  //把图片上传到服务器上
  if (code == 0) {
    // 将资源和人建立连接
    let assetParams = {
      assetLabelSeq: "",
      assetName: requestData.assetName,
      assetType: "URL",
      extension: "url",
      filePath: data.filePath,
      fileSize: data.fileSize,
      labelType: "F",
      srcUrl: data.srcUrl,
      extData: JSON.stringify({
        metaTags: "",
        metaTitle: requestData.metaTitle //TODO
      })
    };
    const finallRes = await createNewAsset(assetParams);
    if (finallRes.code == 0) {
      return { code: 0, _success: true, _msg: "" };
    } else {
      return { code: -1, _success: false, _msg: "资源建立连接失败" };
    }
  } else {
    return { code: -1, _success: false, _msg: "上传base64失败" };
  }
};
// 把资源上传到服务器上

// 创建资源链接
